#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include <QMessageBox>

#define LED_ON "0"
#define LED_OFF "1"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    sockfd(new QTcpSocket (this)),
    aud(new audio(this)),
    sp(new speech),
    timer(new QTimer(this))
{
    ui->setupUi(this);

    led=false;
    ui->stackedWidget->setCurrentIndex(0);

    connect(sockfd, &QTcpSocket::connected, this, &MainWindow::slotConnectSuccess);
    connect(sockfd,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slotConnectError(QAbstractSocket::SocketError)));
    connect(sockfd,&QTcpSocket::readyRead, this, &MainWindow::slotReadData);

    //connect(timer,&QTimer::timeout,this,&MainWindow::slotTimeout);//定时器 连接信号timer和槽函数timeout

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotTimeout() //定时器到期后执行函数
{
    led = !led;
    if(led){
        sockfd->write(LED_ON);
        ui->pushButton_led->setStyleSheet(
                    "QPushButton {"
                    " border-image: url(:/Image/灯.png);"
                    "}"
                    "QPushButton:hover {"
                    "border-image: url(:/Image/灯 (1).png);"
                    "}"
                    "QPushButton:pressed {"
                    "border-image: url(:/Image/灯 (2).png);"
                    "}"
                    );
        timer->start(2 * 1000);
    }else{
        sockfd->write(LED_OFF);
        ui->pushButton_led->setStyleSheet(
                    "QPushButton {"
                    " border-image: url(:/Image/灯 (3).png);"
                    "}"
                    "QPushButton:hover {"
                    "border-image: url(:/Image/灯 (4).png);"
                    "}"
                    "QPushButton:pressed {"
                    "border-image: url(:/Image/灯 (5).png);"
                    "}"
                    );
        timer->start(4 * 1000);
    }
}





void MainWindow::slotReadData()
{
    qDebug() << sockfd->readAll();
//    //服务器读写的槽函数
//       QString text =QString::fromUtf8(sockfd->read(1024));
//       qDebug() << text;
//    //设置ui显示label名称为label_tem
//        ui->label_tem->setText(text);
}

void MainWindow::slotConnectError(QAbstractSocket::SocketError socketError)
{
    Q_UNUSED(socketError);
    QString err = "链接出错:" +sockfd->errorString();
    QMessageBox msgBox;
    msgBox.setText(err);
    msgBox.exec();

    ui->stackedWidget->setCurrentIndex(0);      //出现异常回到登录页面
    sockfd->close();
}

void MainWindow::slotConnectSuccess()
{
    ui->stackedWidget->setCurrentIndex(1);      //页面从0开始，现在到第二页就是1
}

void MainWindow::on_pushButton_login_clicked()
{
    QString ip = ui->lineEdit_ip->text();       //从界面获取ip地址
    quint16 port = ui->lineEdit_port->text().toUShort();        //从界面获取端口号

    if(ip.isEmpty() || port <= 0 ){
        QMessageBox msgBox;
        msgBox.setText("输入IP或端口错误");
        msgBox.exec();
        return;
    }
    QHostAddress addr(ip);      //构建地址对象，填写服务器ip地址
    sockfd->connectToHost(addr,port,QTcpSocket::ReadWrite);//连用用ip和端口，并且以读写权限连接
}

void MainWindow::on_pushButton_led_clicked()
{
    led=!led;
    ui->stackedWidget->setCurrentIndex(3);
    if(led){
//        sockfd->write(LED_ON);
        ui->pushButton_led->setStyleSheet(" QPushButton{"
                                              "border-image: url(:/image/led_on.png);"
                                          "}"
                                          "QPushButton:hover{"
                                              "border-image: url(:/image/led_on1.png);"
                                          "}"
                                          "QPushButton:pressed{"
                                              "border-image: url(:/image/led_on2.png);"
                                          "}");
    }else{
//        sockfd->write(LED_OFF);
        ui->pushButton_led->setStyleSheet(" QPushButton{"
                                              "border-image: url(:/image/led_off.png);"
                                          "}"
                                          "QPushButton:hover{"
                                              "border-image: url(:/image/led_off1.png);"
                                          "}"
                                          "QPushButton:pressed{"
                                              "border-image: url(:/image/led_off2.png);"
                                          "}");
    }
}

void MainWindow::on_pushButton_LED_1_clicked()
{
    int n=0;
    leds[n]=!leds[n];
    leds[4]=false;
    LED_clicked_off(ui->pushButton_LED_all);
    QPushButton *button = ui->pushButton_LED_1;
    sockfd->write("1");
    if(leds[n]){
        LED_clicked_on(button);
    }else {
        LED_clicked_off(button);
    }
}
void MainWindow::on_pushButton_LED_2_clicked()
{
    int n=1;
    leds[n]=!leds[n];
    leds[4]=false;
    LED_clicked_off(ui->pushButton_LED_all);
    QPushButton *button = ui->pushButton_LED_2;
    sockfd->write("2");
    if(leds[n]){
        LED_clicked_on(button);
    }else {
        LED_clicked_off(button);
    }
}
void MainWindow::on_pushButton_LED_3_clicked()
{
    int n=2;
    leds[n]=!leds[n];
    leds[4]=false;
    LED_clicked_off(ui->pushButton_LED_all);
    QPushButton *button = ui->pushButton_LED_3;
    sockfd->write("3");
    if(leds[n]){
        LED_clicked_on(button);
    }else {
        LED_clicked_off(button);
    }
}
void MainWindow::on_pushButton_LED_4_clicked()
{
    int n=3;
    leds[n]=!leds[n];
    leds[4]=false;
    LED_clicked_off(ui->pushButton_LED_all);
    QPushButton *button = ui->pushButton_LED_4;
    sockfd->write("4");
    if(leds[n]){
        LED_clicked_on(button);
    }else {
        LED_clicked_off(button);
    }
}
void MainWindow::on_pushButton_LED_all_clicked()
{
    int n=4;
    leds[n]=!leds[n];
    sockfd->write("5");
    QPushButton *button[] = {ui->pushButton_LED_1,ui->pushButton_LED_2,ui->pushButton_LED_3,ui->pushButton_LED_4,ui->pushButton_LED_all};
    if(leds[n]){
        for(int i=0;i<5;i++){
            leds[i] = true;
            LED_clicked_on(button[i]);
        }
    }else {
        for(int i=0;i<5;i++){
            leds[i] = false;
            LED_clicked_off(button[i]);
        }
    }
}

void MainWindow::LED_clicked_on(QPushButton *button){
    button->setStyleSheet("QPushButton{"
                                          "border-image: url(:/image/led_on.png);"
                                      "}"
                                      "QPushButton:hover{"
                                          "border-image: url(:/image/led_on1.png);"
                                      "}"
                                      "QPushButton:pressed{"
                                          "border-image: url(:/image/led_on2.png);"
                                      "}");
}

void MainWindow::LED_clicked_off(QPushButton *button){
    button->setStyleSheet("QPushButton{"
                                          "border-image: url(:/image/led_on.png);"
                                      "}"
                                      "QPushButton:hover{"
                                          "border-image: url(:/image/led_on1.png);"
                                      "}"
                                      "QPushButton:pressed{"
                                          "border-image: url(:/image/led_on2.png);"
                                      "}");
}

void MainWindow::on_pushButton_voice_pressed()
{
    aud->startRecord("1.pcm");
    ui->pushButton_voice->setText("松开结束");
}

void MainWindow::on_pushButton_voice_released()
{
    aud->stopRecord();
    ui->pushButton_voice->setText("按下说话");

    QString text = sp->speechIdentify("1.pcm");
    qDebug()<< text;
    if(text.contains("定时开关灯")){//查找对应字符串
        timer->start(3 * 1000);   //启动定时器，3秒后重置
    }
    else if(text.contains("关闭定时")) {
        timer->stop();    //停止定时器
        sockfd->write(LED_OFF);
    }
    else if (text.contains("打开音乐播放")) {
        sockfd->write("p");
        ui->stackedWidget->setCurrentIndex(2);
    }
    else if (text.contains("打开电子琴")) {
        sockfd->write("z");
        ui->stackedWidget->setCurrentIndex(3);
    }
}



void MainWindow::on_pushButton_music_clicked()
{
    sockfd->write("q");
}

void MainWindow::on_pushButton_keyboard_clicked()
{
    sockfd->write("z");
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_Do_pressed()
{
    sockfd->write("a");
}

void MainWindow::on_pushButton_Do_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_re_pressed()
{
    sockfd->write("s");
}

void MainWindow::on_pushButton_re_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_mi_pressed()
{
    sockfd->write("d");
}

void MainWindow::on_pushButton_mi_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_fa_pressed()
{
    sockfd->write("f");
}

void MainWindow::on_pushButton_fa_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_sol_pressed()
{
    sockfd->write("g");
}

void MainWindow::on_pushButton_sol_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_la_pressed()
{
    sockfd->write("h");
}

void MainWindow::on_pushButton_la_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_si_pressed()
{
    sockfd->write("j");
}

void MainWindow::on_pushButton_si_released()
{
    sockfd->write("k");
}

void MainWindow::on_pushButton_clicked()
{
    sockfd->write("p");
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_pushButton_tem_clicked()
{
    //获取温度
        sockfd->write("X");
}
